#!/usr/bin/python3
import json
import sys
import mytool.zabbix.discovery_storage.db as db

value_map = {
    'name': 0,
    'free': 1,
    'capacity': 2
}


def get_json(storage):
    data = []
    for s in storage:
        data.append({"{#STORAGE_NAME}":s[0]})
    j = json.dumps({'data': data}, sort_keys=True, indent=4, separators=(',', ':'))
    return j


def get_data(option, arg):
    storage_data = []
    args = arg.split(',')
    mssql = db.MSSQL()
    for l in mssql.get_storage():
        single_data = []
        if option == 'st' and 'DB' in l[0]:
            continue
        elif option == 'db' and 'DB' not in l[0]:
            continue
        for a in args:
            single_data.append(l[value_map[a]])
        storage_data.append(single_data)
    return storage_data


if __name__ == "__main__":
    args = sys.argv[1]
    name = get_data(args, 'name')
    j = get_json(name)
    print(j)